// components/SelectDates/index.js
Component({
  options: {
    addGlobalClass: true,
  },
  properties: {
    displayMonth:{
      type: String,
      value: new Date().toLocaleDateString()
    },
    minDate: String,
    maxDate: String,
    selectedDates: Array
  },
  data: {
    show: false,
    // type: 0.none, 1.disabled,2.selected,3.unselect
    datesList:[],
    week: ["日", "一", "二", "三", "四", "五", "六"],
    currentMM:null,
    currentYY:null,
    selectedDates: [],
    memDates:[]
  },
  methods: {
    open() {
      this.setData({
        show:true
      })
    },
    close() {
      this.setData({
        show:false
      })
    },
    initData() {
      const displayYY = this.data.currentYY
      const displayMM = this.data.currentMM
      const displayWeek = new Date(`${displayYY}/${displayMM}/1`).getDay()
      const minDate = new Date(this.properties.minDate)
      const maxDate = new Date(this.properties.maxDate)
      let tmpDatesList = []
      let weekDays = [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31]

      if (displayYY % 4 == 0 && displayYY % 100 != 0 || displayYY % 400 == 0) {
        weekDays[1] = 29
      } else {
        weekDays[1] = 28
      }

      for (let i = 0; i < displayWeek; i++) {
        tmpDatesList.push({
          type: "none", content: ""
        })
      }

      for (let i = 1; i <= weekDays[displayMM - 1]; i++) {
        let currentDay = new Date(`${displayYY}/${displayMM}/${i}`)

        if (currentDay < minDate || currentDay > maxDate) {
          tmpDatesList.push({
            type: "disabled", content: `${i}`
          })
        } else if (this.data.selectedDates.find((i) => {
          return new Date(i).toLocaleDateString() == currentDay.toLocaleDateString()
        })) {
          tmpDatesList.push({
            type: "selected", content: `${i}`
          })
        } else {
          tmpDatesList.push({
            type: "unselect", content: `${i}`
          })
        }
      }

      this.setData({
        datesList: tmpDatesList
      })
    },
    toLastMonth() {
      if(this.data.currentMM == 1) {
        this.setData({
          currentMM: 12,
          currentYY: this.data.currentYY - 1
        })
      } else {
        this.setData({
          currentMM: this.data.currentMM - 1,
        })
      }
      this.initData()
    },
    toNextMonth() {
      if (this.data.currentMM != 12) {
        this.setData({
          currentMM: this.data.currentMM + 1,
        })
      } else {
        this.setData({
          currentMM: 1,
          currentYY: this.data.currentYY + 1
        })
      }
      this.initData()
    },
    toCancel() {
      this.setData({
        selectedDates: this.data.memDates
      })
      this.initData()
      this.close()
    },
    toConfirm() {
      this.triggerEvent("confirm", this.data.selectedDates)
    },
    toSelectDay(e) {
      let type = e.target.dataset.hi.type
      let day = e.target.dataset.hi.content

      let tmpMM = (this.data.currentMM >= 10) ? `${this.data.currentMM}` : `0${this.data.currentMM}`
      let tmpDD = (parseInt(day) >= 10) ? `${day}` : `0${day}`
      let tmp = `${this.data.currentYY}-${tmpMM}-${tmpDD}`

      if (type == "none" || type == "disabled") {
        return
      } else if (type == "unselect") {
        this.data.selectedDates.push(tmp)
        this.initData()
      } else {
        let tmpIdx = this.data.selectedDates.indexOf(tmp)
        if(tmpIdx != -1) {
          this.data.selectedDates.splice(tmpIdx, 1)
          this.initData()
        }  
      }
    }
  },
  attached() {
    const displayYY = new Date(this.properties.displayMonth).getFullYear()
    const displayMM = new Date(this.properties.displayMonth).getMonth() + 1
    const tmp = this.properties.selectedDates.slice(0)
    this.setData({
      currentMM: displayMM,
      currentYY: displayYY,
      selectedDates: this.properties.selectedDates,
      memDates: tmp,
    })
    this.initData() 
  }
})
